package com.v2future.sjt.collegecheckingattendance.utils;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * 获取手机的一些基本信息 终端唯一标示ID IMEI 终端应用版本 应用版本 终端品牌 Apple， Samsung 终端机型 Note3，iPhone
 * 5S 终端系统类型 iOS 7，Android 4.4 终端网络制式 WiFi 3G 2G LTE IP信息
 * 
 * @author Pow
 * 
 */
public class SystemInfoUtil {

	private static SystemInfoUtil instance = null;
	private Context c;
	static TelephonyManager ttsTelInfo;

	public static SystemInfoUtil getInstance(Context c) {
		if (instance == null) {
			instance = new SystemInfoUtil();
			instance.c = c.getApplicationContext();
			ttsTelInfo = (TelephonyManager) c
					.getSystemService(Context.TELEPHONY_SERVICE);
		}
		return instance;
	}

	private SystemInfoUtil() {
	}

	/**
	 * 获取IMEI号
	 */
	public String getImei() {
		return ttsTelInfo.getDeviceId();
	}

	/**
	 * 获取版本号
	 */
	public String getAppVersionName() {
		String versionName = "";
		try {
			PackageManager packageManager = c.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					c.getPackageName(), 0);
			versionName = packageInfo.versionName;
			if (TextUtils.isEmpty(versionName)) {
				return "版本号暂无";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "版本号暂无";
		}
		// return c.getString(R.string.app_name)+versionName;
		return versionName;
	}

	/**
	 * 获取手机品牌
	 */
	public String getBrand() {
		return android.os.Build.BRAND;
	}

	/**
	 * 获取手机机型
	 */
	public String getPhoneType() {

		return android.os.Build.MODEL.replace(" ", ""); // 手机机型

	}

	/**
	 * 获取手机的系统的版本号
	 */
	public int getSdkVersion() {

		return android.os.Build.VERSION.SDK_INT;
	}

	/**
	 * 获取手机的网络型号
	 */
	public String getNetworkType() {
		ConnectivityManager connManager = (ConnectivityManager) c
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = connManager.getActiveNetworkInfo();
		String networkType = "";
		if (networkinfo != null) {
			networkType = networkinfo.getTypeName();
		}
		return networkType;
	}

	/**
	 * 获取手机的ip地址
	 */
	public String getPhoneIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()
							&& inetAddress instanceof Inet4Address) {
						// if (!inetAddress.isLoopbackAddress() && inetAddress
						// instanceof Inet6Address) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
		}
		return "";
	}

	/**
	 * 获取手机的分辨率
	 * 
	 * @return
	 */
	public String getScreenDisplay(Activity activity) {
		String str = "";
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int widthPixels = dm.widthPixels;
		int heightPixels = dm.heightPixels;
		float density = dm.density;
		int screenWidth = (int) (widthPixels * density);
		int screenHeight = (int) (heightPixels * density);
		str = screenWidth + "x" + screenHeight;
		str = widthPixels + "x" + heightPixels;
		return str;
	}

	public int getScreenWidth(Context context) throws Exception {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		return width;
	}

	public int getScreenHeight(Context context) throws Exception {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getHeight();
		return width;
	}

	/**
	 * 获取application下面的meta-data的值
	 * 
	 * @param context
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public String getAppMetaStr(Context context, String key) throws Exception {
		ApplicationInfo appInfo = context.getPackageManager()
				.getApplicationInfo(context.getPackageName(),
						PackageManager.GET_META_DATA);
		String msg = appInfo.metaData.getString(key);
		return msg;
	}

	/**
	 * 获取当前设置的电话号码,并不是都能获取到
	 */
	public String getNativePhoneNumber(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String NativePhoneNumber = null;
		NativePhoneNumber = telephonyManager.getLine1Number();
		return NativePhoneNumber;
	}

	/**
	 * 获取手机服务商信息
	 */
	public String getProvidersName(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String ProvidersName = null;
		// 返回唯一的用户ID;就是这张卡的编号神马的
		String IMSI = telephonyManager.getSubscriberId();
		// IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
		LogUtils.d(IMSI);
		if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
			ProvidersName = "中国移动";
		} else if (IMSI.startsWith("46001")) {
			ProvidersName = "中国联通";
		} else if (IMSI.startsWith("46003")) {
			ProvidersName = "中国电信";
		}
		return ProvidersName;
	}

	/**
	 * 获取手机CPU信息
	 * 
	 * @return
	 */
	public String getCpuInfo() {
		String str1 = "/proc/cpuinfo";
		String str2 = "";
		String[] cpuInfo = { "", "" };
		String[] arrayOfString;
		try {
			FileReader fr = new FileReader(str1);
			BufferedReader localBufferedReader = new BufferedReader(fr, 8192);
			str2 = localBufferedReader.readLine();
			arrayOfString = str2.split("\\s+");
			for (int i = 2; i < arrayOfString.length; i++) {
				cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " ";
			}
			str2 = localBufferedReader.readLine();
			arrayOfString = str2.split("\\s+");
			cpuInfo[1] += arrayOfString[2];
			localBufferedReader.close();
		} catch (IOException e) {
		}
		return "CPU型号 " + cpuInfo[0] + "," + "CPU频率: " + cpuInfo[1];
	}

	/**
	 * 获取wlan mac地址
	 * 
	 * @author perfect-sjt
	 * @param context
	 * @return
	 */
	public String getWlanMacAddr(Context context) {
		String result = "";
		WifiManager wm = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		result = wm.getConnectionInfo().getMacAddress();
		return result;
	}

	/**
	 * 获取蓝牙mac地址
	 * 
	 * @author perfect-sjt
	 * @param context
	 * @return
	 */
	public String getBTMacAddr(Context context) {
		String result = "";
		BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter
				.getDefaultAdapter();
		if (m_BluetoothAdapter != null) {
			return m_BluetoothAdapter.getAddress();
		}

		return result;
	}
}
